import github from "../api/github"
export default {
  searchName(store,username){
    if(username){
      store.commit("searchName",username)
    }
  },

  async getList(store){
    try{
      //显示loading效果
      store.commit("showLoding")

      //发送请求获取list数据  调用mutation修改仓库数据
      const {items} = await github.get("/search/users",
        {params:{q:store.state.username}
        })

      const list = items.map(user => ({
        ...user,
        home:user.html_url,
        avatar:user.avatar_url,
        name:user.login
      }));

      if(list.length>0){
        store.commit("showList")
      }else{
        store.commit("showNoBody")
      }

      //将list入库
      store.commit("getList",list)
      //清空username
      store.commit("searchName","")
    }catch (e) {
      //e.message : 错误信息
      store.commit("showError",e.message)
    }
  }
}
